package com.ruoyi.web.controller.Region;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.OlivePlantArea;
import com.ruoyi.system.service.IOlivePlantAreaService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 种植区信息Controller
 * 
 * @author ruoyi
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/region/plantarea")
public class OlivePlantAreaController extends BaseController
{
    @Autowired
    private IOlivePlantAreaService olivePlantAreaService;

    /**
     * 查询种植区信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:area:list')")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(OlivePlantArea olivePlantArea)
    {
        startPage();
        List<OlivePlantArea> list = olivePlantAreaService.selectOlivePlantAreaList(olivePlantArea);
        return getDataTable(list);
    }

    /**
     * 导出种植区信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:export')")
    @Log(title = "种植区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OlivePlantArea olivePlantArea)
    {
        List<OlivePlantArea> list = olivePlantAreaService.selectOlivePlantAreaList(olivePlantArea);
        ExcelUtil<OlivePlantArea> util = new ExcelUtil<OlivePlantArea>(OlivePlantArea.class);
        util.exportExcel(response, list, "种植区信息数据");
    }

    /**
     * 获取种植区信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:query')")
    @GetMapping(value = "/{plantAreaId}")
    public AjaxResult getInfo(@PathVariable("plantAreaId") Long plantAreaId)
    {
        return success(olivePlantAreaService.selectOlivePlantAreaByPlantAreaId(plantAreaId));
    }

    /**
     * 新增种植区信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:add')")
    @Log(title = "种植区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OlivePlantArea olivePlantArea)
    {
        if(!olivePlantAreaService.checkOlivePlantAreaIdUnique(olivePlantArea)){
            return error("新增种植区"+olivePlantArea.getPlantAreaId()+"'失败，种植区编码已存在'");
        }
        olivePlantArea.setCreateBy(getUsername());
        AjaxResult ajaxResult = toAjax(olivePlantAreaService.insertOlivePlantArea(olivePlantArea));
        return ajaxResult;
    }

    /**
     * 修改种植区信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:edit')")
    @Log(title = "种植区信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody OlivePlantArea olivePlantArea)
    {
        olivePlantArea.setUpdateBy(getUsername());
        return toAjax(olivePlantAreaService.updateOlivePlantArea(olivePlantArea));
    }

    /**
     * 删除种植区信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:remove')")
    @Log(title = "种植区信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{plantAreaIds}")
    public AjaxResult remove(@PathVariable Long[] plantAreaIds)
    {
        return toAjax(olivePlantAreaService.deleteOlivePlantAreaByPlantAreaIds(plantAreaIds));
    }
}
